<template>
  <div class="app-container">
    <el-tabs type="border-card">
      <el-tab-pane label="热浪">
        <div class="filter-container">
          <el-button
            class="filter-item"
            type="success"
            icon="el-icon-edit"
            @click="handleAddAccount"
          >
            添加账号
          </el-button>
        </div>
        <el-table
          ref="dragTable"
          v-loading="listLoading"
          :data="list"
          border
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column min-width="300px" label="淘宝账号ID">
            <template slot-scope="{ row }">
              <span>{{ row.account_id }}</span>
            </template>
          </el-table-column>

          <el-table-column width="200px" label="淘宝昵称">
            <template slot-scope="{ row }">
              <span>{{ row.account_name }}</span>
            </template>
          </el-table-column>

          <el-table-column min-width="200px" label="账号备注">
            <template slot-scope="{ row }">
              <span>{{ row.account_remark }}</span>
            </template>
          </el-table-column>

          <el-table-column min-width="200px" label="授权到期时间">
            <template slot-scope="{ row }">
              <span>{{ row.expired_at }}</span>
            </template>
          </el-table-column>

          <el-table-column min-width="200px" label="启用状态">
            <template slot-scope="{ row }">
              <el-switch
                v-model="row.status"
                :active-value="1"
                :inactive-value="2"
                @change="handleChange($event, row.id)"
              />
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('table.actions')"
            align="center"
            width="230"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                size="mini"
                @click="handleAuthAgain(row)"
              >
                重新授权
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="淘宝联盟" />
    </el-tabs>
  </div>
</template>

<script>
import {
  fetchAccountList,
  accountEdit,
  accountAuthUrl,
  accountRefresh
} from '@/api/index'
export default {
  data() {
    return {
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        type: 1
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchAccountList(this.listQuery).then((response) => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleChange(val, id) {
      accountEdit({ id, status: val }).then(() => {
        this.getList()
      })
    },
    handleAddAccount() {
      accountAuthUrl().then((res) => {
        window.open(res.data.url)
      })
    },
    handleAuthAgain(row) {
      accountRefresh({ id: row.id }).then(() => {
        this.getList()
      })
    }
  }
}
</script>

<style>
.sortable-ghost {
  opacity: 0.8;
  color: #fff !important;
  background: #42b983 !important;
}
</style>

<style scoped>
.icon-star {
  margin-right: 2px;
}
.drag-handler {
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d {
  margin-top: 15px;
}
</style>
